import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

# 创建浏览器驱动对象
chrome_driver="../driver/chromedriver.exe"

service=Service(executable_path=chrome_driver)
# 指向服务端是本地驱动
driver=webdriver.Edge()
driver.maximize_window()
# driver.implicitly_wait(10)
#打开页面
driver.get("http://localhost:82/index.php")

driver.find_element(By.CSS_SELECTOR,'.red').click()
driver.find_element(By.CSS_SELECTOR,'#username').send_keys("13800138006")
driver.find_element(By.CSS_SELECTOR,'#password').send_keys("123456")
driver.find_element(By.CSS_SELECTOR,'#verify_code').send_keys("8888")
driver.find_element(By.CSS_SELECTOR,"a[name='sbtbutton']").click()
time.sleep(2)
gouwuche=driver.find_element(By.CSS_SELECTOR,"body > div:nth-child(8) > div:nth-child(1) > div:nth-child(4) > a:nth-child(1)")
print(gouwuche.get_attribute("href"))
# gouwuche.click()

driver.find_element(By.CSS_SELECTOR,"#hd-my-cart").click()
# goods_num=driver.find_element(By.CSS_SELECTOR,"#goods_num")
time.sleep(2)
goods_num=(WebDriverWait(driver,10).until(lambda x:x.find_element(By.CSS_SELECTOR,'#goods_num'))) #x:就是驱动对象


# 平商品的平均价格


# time.sleep(2)
goods_nums=goods_num.text   #这一个问题,稍后解释,为啥需要让代码休眠?
print("开发给的:",goods_nums)

input_list=driver.find_elements(By.CSS_SELECTOR,'[class="column t-quantity mtp quantity-form"] input')
sum_goods_num=0
for ele in input_list:
    sum_goods_num+=int(ele.get_attribute("value"))
print("自己计算的:",sum_goods_num)
time.sleep(10)



#关闭浏览器驱动对象
driver.quit()
